package purchase

import (
	"LogisticsPort/model"
	"LogisticsPort/sql"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"sort"
	"time"
)

func agent(r *gin.RouterGroup) {
	agent := r.Group("/agent")
	{
		agent.GET(sql.RequestList, purchaseAgentList)
		agent.GET(sql.RequestData, purchaseAgentInfo)
	}
}

func purchaseAgentInfo(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var list []model.PurchaseProduct
		db.Preload("ShippingInfo").Preload("PurchaseSales").Find(&list)
		var divideNumber = 0    //拆柜数量
		var stockNumber = 0     //库存数量
		var salesNumber = 0     //已出库数量
		var transportNumber = 0 //运输数量
		for _, product := range list {
			if len(product.PurchaseSales) > 0 { //有销售信息，1、整出,2、拆柜出
				var agentDivideNumber = 0
				for _, sale := range product.PurchaseSales {
					agentDivideNumber = agentDivideNumber + sale.SalesNumber
				}
				if agentDivideNumber != product.ProductNumber {
					divideNumber += 1
				} else {
					salesNumber += 1
				}
			} else if product.ShippingInfo != nil { //有船务信息
				if (time.Time)(product.ShippingInfo.WarehousingDate).IsZero() { //出库时间为空，没出库
					transportNumber += 1
				} else {
					stockNumber += 1
				}
			} else {
				transportNumber += 1
			}
		}
		var resultList model.PurchaseAgent
		resultList.SalesNumber = salesNumber
		resultList.StockNumber = stockNumber
		resultList.DivideNumber = divideNumber
		resultList.TransportNumber = transportNumber
		return db, resultList
	})
}

// PurchaseAgentList /** 代理列表
func purchaseAgentList(c *gin.Context) {
	sql.HttpHandle(sql.GET, c, func(db *gorm.DB) (*gorm.DB, interface{}) {
		var list []model.PurchaseAgent
		db.Preload("PurchaseProduct", func(db *gorm.DB) *gorm.DB {
			db = db.
				Select("id, agent_id, product_number").
				Preload("ShippingInfo", func(db *gorm.DB) *gorm.DB {
					return db.Select("id, purchase_product_id, warehousing_date")
				}).
				Preload("PurchaseSales", func(db *gorm.DB) *gorm.DB {
					return db.Select("id, purchase_product_id, sales_number")
				})
			return db
		}).Find(&list)
		var resultList []model.PurchaseAgent
		for _, purchaseAgent := range list {
			var divideNumber = 0    //拆柜数量
			var stockNumber = 0     //库存数量
			var salesNumber = 0     //已出库数量
			var transportNumber = 0 //运输数量
			for _, product := range purchaseAgent.PurchaseProduct {
				if len(product.PurchaseSales) > 0 { //有销售信息，1、整出,2、拆柜出
					var agentDivideNumber = 0
					for _, sale := range product.PurchaseSales {
						agentDivideNumber = agentDivideNumber + sale.SalesNumber
					}
					if agentDivideNumber != product.ProductNumber {
						divideNumber += 1
					} else {
						salesNumber += 1
					}
				} else if product.ShippingInfo != nil { //有船务信息
					if (time.Time)(product.ShippingInfo.WarehousingDate).IsZero() { //出库时间为空，没出库
						transportNumber += 1
					} else {
						stockNumber += 1
					}
				} else {
					transportNumber += 1
				}
			}
			purchaseAgent.StockNumber = stockNumber
			purchaseAgent.SalesNumber = salesNumber
			purchaseAgent.DivideNumber = divideNumber
			purchaseAgent.TransportNumber = transportNumber
			purchaseAgent.PurchaseProduct = nil
			resultList = append(resultList, purchaseAgent)
		}
		if len(resultList) > 0 {
			less := func(i, j int) bool {
				a, b := &resultList[i], &resultList[j]
				switch {
				case a.StockNumber != b.StockNumber:
					return a.StockNumber > b.StockNumber
				case a.DivideNumber != b.DivideNumber:
					return a.DivideNumber > b.DivideNumber
				case a.TransportNumber != b.TransportNumber:
					return a.TransportNumber > b.TransportNumber
				default:
					return a.SalesNumber > b.SalesNumber
				}
			}
			sort.Slice(resultList, less)
		}
		return db, resultList
	})
}
